/* Copyright (c) 2011 by Mani Brundha Kattamuri, Haritha Subramanyam
 *
 * Project Info:  http://code.google.com/p/andriod-travelapp-csc780/wiki/Design
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */
package com.example.TravelApp;

import java.util.ArrayList;

import com.example.TravelApp.search.advanced.AdvancedActivity;
import com.example.TravelApp.search.advanced.AdvancedTabOptionDetailsActivity;
import com.example.TravelApp.search.advanced.SavedOptionActivity;
import com.example.TravelApp.search.common.PlaceObject;
import com.example.TravelApp.search.common.PlacesSharedPreference;
import com.example.TravelApp.search.quick.QuickSearchActivity;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

/**
 * This activity is the main activity that is called when the application is
 * launched. It contains two tabs : 1) QUick Search 2) Advanced Search The quick
 * search is the first tab and if the user chooses to perform advanced search
 * the advanced tab needs to be selected
 * 
 * @author Haritha Subramanyam, Mani Brundha
 * 
 */

public class TravelAppActivity extends TabActivity implements
		OnTabChangeListener {
	/** Called when the activity is first created. */
	TabHost host;

	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		String savedPlaces = PlacesSharedPreference.loadStringbject(
				getBaseContext(), "mySavedPlaces");
		ArrayList<PlaceObject> recievedList = null;
		if (savedPlaces != "" && savedPlaces != null) {
			if (((ArrayList<PlaceObject>) PlaceObject.toString(savedPlaces))
					.isEmpty() != true) {
				recievedList = (ArrayList<PlaceObject>) PlaceObject
						.toString(savedPlaces);
			}

		}
		Resources r = getResources();
		host = getTabHost();

		// Tab displaying Quick Search activity
		host.addTab(host.newTabSpec("QuickSearch")
				.setIndicator("QuickSearch", r.getDrawable(R.drawable.tab_bg))
				.setContent(new Intent(this, QuickSearchActivity.class)));
		host.getTabWidget().getChildAt(0).getLayoutParams().height = 55;
		// Tab displaying Advanced Search activity
		host.addTab(host
				.newTabSpec("AdvancedSearch")
				.setIndicator("AdvancedSearch",
						r.getDrawable(R.drawable.tab_bg))
				.setContent(
						new Intent(this, AdvancedActivity.class).putExtra(
								"TAB", "AdvancedSearch")));

		host.getTabWidget().getChildAt(1).getLayoutParams().height = 55;

		// Tab displaying details of all the places included in the trip
		// plan.
		host.addTab(host
				.newTabSpec("Saved Option")
				.setIndicator("Saved Option", r.getDrawable(R.drawable.tab_bg))
				.setContent(
						new Intent(this, SavedOptionActivity.class)
								.putParcelableArrayListExtra("placesList",
										recievedList)));
		host.getTabWidget().getChildAt(2).getLayoutParams().height = 55;
		// set listener to tabhost IMPORTANT IMPORTANT
		host.setOnTabChangedListener(this);

		tabStyling(); // changes tab styles

		for (int i = 0; i < host.getTabWidget().getChildCount(); i++) {
			host.getTabWidget().getChildAt(i)
					.setBackgroundResource(R.drawable.tab_bg); // unselected
		}
		host.getTabWidget().getChildAt(host.getCurrentTab())
				.setBackgroundResource(R.drawable.tab_bg); // selected

	}

	/**
	 * Modifies the styling of the tabs when the tab change activity occurs
	 */
	@Override
	public void onTabChanged(String tabId) {
		for (int i = 0; i < host.getTabWidget().getChildCount(); i++) {
			host.getTabWidget().getChildAt(i)
					.setBackgroundResource(R.drawable.tab_bg);

		}
		host.getTabWidget().getChildAt(host.getCurrentTab())
				.setBackgroundResource(R.drawable.tab_bg);

	}

	/**
	 * 
	 */
	private ArrayList<PlaceObject> getParcableList() {
		ArrayList<PlaceObject> sortedList = new ArrayList<PlaceObject>();
		return sortedList;

	}

	/**
	 * Adds custom styling to the tab by using drawable resources
	 */
	private void tabStyling() {
		// change tab background color to red
		for (int i = 0; i < host.getTabWidget().getChildCount(); i++) {
			host.getTabWidget().getChildAt(i)
					.setBackgroundResource(R.drawable.tab_bg);
			View tempView = host.getTabWidget().getChildAt(i);
		}

		// o set different color for current selected tab to blue
		host.getTabWidget().getChildAt(host.getCurrentTab())
				.setBackgroundResource(R.drawable.tab_bg);

	}
}